 
import { ABaseDecision } from "GameLoop/DecisionMaking/BaseDecision";
import { WarTeamType } from "GameLoop/WorldState/EntityManager/Entitys/Entity.interface";
 
import { CacheDataHostiles } from "GameLoop/WorldState/PollingStation/PollingTasks/RoomItem/CacheHostiles";
import { priority } from "utils/constants/definetypes";
 
import { CreepAbilityHelper } from "utils/creep/CreepAbilityHelper";
import { BodySetup } from "utils/creep/CreepSetup";
import { BodyUtils } from "utils/creep/bodyutils";
import { Roles } from "utils/creep/setups";
import { UtilsHelper } from "utils/position/UtilsHelper";
import { TaskHelper } from "utils/task/TaskHelper";
import { generateUniqueId } from "utils/utils";
import { WarStrategy } from "utils/war/WarStrategy";

interface IEnemyStatus
{
    sum_heal: number;
    sum_damage: number;
    enemy_count: number;
    hostileCreeps: Creep[];
}
/**
 * 野外资源房战斗
 */
export class ResourceRoomWarDecision< T extends "team"|"room"="team"|"room"> extends ABaseDecision<T>
{
   
    public isExpired(event: NormalEvent, region: string): boolean 
    {
        
        const room = Game.rooms[event.region] as Required<Room>;
        if(room.controller.level<2 )
        {
            return false
        }

        return true;
    }
    public makeTask(event: NormalEvent, region: string): IDecisionCode<T>|false
    {
      
        const room = Game.rooms[event.region] as Required<Room>;
        const existtask = room.findTaskById<ITask<ITeamTaskData<"defend">>>(`${event.region}_mine_war`,"team");
        if(existtask)
        {
            if(!existtask.data.arg.benchRooms.includes(event.room))
            {
                existtask.data.arg.benchRooms.push(event.room);
            }
            if(room.canExecute(1187))
            {
                // 修改plan
                const task = this.makeDefendTask(event);
                if(task)
                {
                    existtask.data.creepRequired = task.data.creepRequired;
                    existtask.data.required = task.data.required;
                }
            }
            return false;
        }
        const task = this.makeDefendTask(event);
        if(task)
        {
            const code:IDecisionCode<T>={
                region:event.region,
                isunique:false,
                task:[task as any ],
            }
            return code
        }
  
        return false;
        // return code
    }
    
    public makeDefendTask(event: NormalEvent  )
    {
        
        const room = Game.rooms[event.region] as Required<Room>;
        const hostcreeps  =room.getWorld().PollHostiles(event.room);
        
        if(!hostcreeps)
        {
            
            return undefined
        }
        let plan:IWarPlan|undefined;
        const hostileCreeps:Creep[]=[];
        let structures:Structure[]=[];
        if(hostcreeps.cacheData && hostcreeps.cacheData.length>0)
        {
             
            for(const e of hostcreeps.cacheData)
            {
                const creep = Game.getObjectById(e.id);
                if(creep)
                {
                    hostileCreeps.push(creep);
                }
            }
     
            // 资源房的建筑只能是 npc core
            if(hostileCreeps.length>0)
            {
                // 守外矿不用boost
                plan =WarStrategy.createPlan(room,hostileCreeps,0);
                if(!plan)
                {
                    return false
                }
            }
        }
        else 
        {
            structures = Game.rooms[event.room].find(FIND_STRUCTURES,{
                filter: { structureType: STRUCTURE_INVADER_CORE }}) ;
            if(structures.length>0)
            {
                plan = WarStrategy.createPlanOfDestoryStructure(room) 
            }
                
        }
        if(!plan)
        {
            return false;
        }
        const creepnum = _.sum(plan.creep,e=>e[1]);
        if(creepnum>1)
        {
            const team2Config:ITeamConfig={
                type:"Team2",
                creep:[
                    [
                        {
                            Role:"idaRed",
                            
                            body:[ [MOVE,25],[ATTACK,20],[RANGED_ATTACK,5]],
                            isAdapt:true,
                        },
                        1
                    ],
                    [
                        {
                            Role:"idaHeal",
                            
                            body:[  [MOVE,14], [HEAL,12],[MOVE,1]],
                            isAdapt:true,
                        },
                        1
                    ],
                ]
            };
             if(creepnum==2||creepnum==3)
            {
                plan.creep=[];
                plan.team=[[team2Config,1]]
            }
            else  if(creepnum==4||creepnum==5)
            {
                plan.creep=[];
                plan.team=[[team2Config,2]]
            }
            else 
            {
                // 放弃抵抗
                plan.team=[]
                plan.creep=[];
            }
        }
        const wartask:ITeamTaskData<"defend">  ={
            type:"defend",
            room:event.room,
            arg:{
                benchRooms:[],
                enemy: hostileCreeps.map(e=>e.id),
                structures:structures?structures.map(e=>e.id) as any[]:[],
            },
            cost:TaskHelper.getTaskPathCost(room.name,event.room),
            creeps:[],
            teams:{} as any,
            creepRequired: plan.creep,
            required:plan.team,
        }
        const teamTask:ITaskDeclare["team"]=
        {
            id:`${event.region}_mine_war`,
            priority: priority.A,
            ttl: 2000,
            type: "team",
            data: wartask,
            shard: 0,
            condition:{
                type:"timeout",
            }
        }
        return teamTask;
       
        // return undefined
    }
 
}